package com.gx.smart.building.ui.maintain.fragment

import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.findNavController
import com.gx.smart.base.BaseAdapter
import com.gx.smart.building.R
import com.gx.smart.building.databinding.FragmentMaintainTaskCompleteBinding
import com.gx.smart.building.ui.common.popwindow.RegionPopWindow
import com.gx.smart.building.ui.maintain.adapter.TaskRecordAdapter
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.dto.PlanTaskAssetDto
import com.huishi.gx.ib.grpc.lib.sysuser.sysregionapi.SysRegionDto
import razerdp.basepopup.QuickPopupBuilder
import razerdp.basepopup.QuickPopupConfig

class MaintainTaskCompleteFragment : CommonFragment(),
    BaseAdapter.OnItemClickListener<PlanTaskAssetDto> {
    private lateinit var viewBinding: FragmentMaintainTaskCompleteBinding
    private lateinit var adapter: TaskRecordAdapter
    private var areaId: String? = null
    private var regionList: List<SysRegionDto>? = null

    override fun onItemClick(e: PlanTaskAssetDto, position: Int) {
        findNavController().navigate(R.id.action_maintainDetailFragment_to_maintainSubmitFragment,
            Bundle().apply {
                putString("planTaskId", e.planTaskId)
                putString("assetId", e.assetId)
            }
        )
    }

    override fun onBindLayout(): Int = R.layout.fragment_maintain_task_complete

    override fun initContentView(root: ViewGroup) {
        viewBinding = FragmentMaintainTaskCompleteBinding.inflate(layoutInflater, root, true)
        viewBinding.filter.setOnClickListener {
            QuickPopupBuilder.with(context)
                .contentView(R.layout.inspection_task_popwindow_layout)
                .config(
                    QuickPopupConfig()
                        .gravity(Gravity.BOTTOM or Gravity.CENTER_VERTICAL)
                        .withClick(
                            R.id.all,
                            { v1: View? ->
                                all()
                            }, true
                        )
                        .withClick(
                            R.id.area,
                            { v1: View? ->
                                areaPicker()
                            }, true
                        )
                )
                .show(it)
        }
    }

    private fun all() {
        this.areaId = ""
        viewBinding.filterContent.text = ""
        if (this::viewBinding.isInitialized) {
            viewBinding.refreshLayout.autoRefresh()
        }
    }

    private fun areaPicker() {
        RegionPopWindow(requireContext())
            .setOnItemSelectedListener { areaId, areaName ->
                this.areaId = areaId
                viewBinding.filterContent.text = areaName
                if (this::viewBinding.isInitialized) {
                    viewBinding.refreshLayout.autoRefresh()
                }
            }.setData(regionList)
            .setBackground(requireContext().getDrawable(R.drawable.background_color))
            .setPopupGravity(Gravity.BOTTOM)
            .showPopupWindow()
    }


    override fun initContent() {
        super.initContent()
        adapter = TaskRecordAdapter(activity)
        adapter.setItemClickListener(this)
        initView(viewBinding.recyclerView, adapter, viewBinding.refreshLayout)
    }

    override fun doAction() {
        requireActivity().intent.getStringExtra("id")?.let {
            viewModel.queryPlanTaskAssetList(currentPage, 1, it, keyword ?: "", "")
                .observe(viewLifecycleOwner) { list ->
                    handleResult(list)
                }
        }
    }

    override fun search(keyword: String?) {
        super.search(keyword)
        if (this::viewBinding.isInitialized) {
            viewBinding.refreshLayout.autoRefresh()
        }
    }

    override fun initData() {
        super.initData()
        doAction()
        viewModel.sysRegionQuery().observe(viewLifecycleOwner) {
            regionList = it
        }
    }


    companion object {
        @JvmStatic
        fun newInstance() = MaintainTaskCompleteFragment()
    }
}